﻿using System;
using System.Collections.Generic;
using System.Text;
using VideoCache.Aggregation.Interfaces;
using System.Text.RegularExpressions;
using Fsb.Utility;

namespace VideoCache.Aggregation.Implements
{
    [Site("sohu.com")]
    class SohuFetcher
    {
        public string GetToken(string url)
        {
            string token = this.GetTokenByUrl(url);

            if (string.IsNullOrEmpty(token))
            {
                token = this.GetTokenByPlayer(url);
            }

            return token;
        }

        private string GetTokenByUrl(string url)
        {
            string pattern = "^(?<Channel>.*).sohu.com/(?<Random>.*)/n(?<Id>.*).shtml";

            Match match = Regex.Match(url, pattern);

            if (match.Success)
            {
                return match.Groups["Id"].Value;
            }

            return null;
        }

        private string GetTokenByPlayer(string url)
        {
            // http://tv.sohu.com/upload/swf/20130411/Main.swf?imgCutBtn=1&nid=372911483&pid=359773552&plid=5136409&pub_catecode=&skin=0&domain=inner&cover=http://photocdn.sohu.com/20130413/vrsb807606.jpg&pageurl=http://tv.sohu.com/20130416/n372911483.shtml&vid=1091021&sid=1304151650398695&jump=0&shareBtn=1&cinemaBtn=1&playListId=1&miniWinBtn=1&lightBtn=1&widerBtn=1&onPlayed=sohuHD.onVideoPlayed&onPlay=sohuHD.onVideoPlay&onPause=sohuHD.onVideoPause&downloadBtn=1&cmscat=251366993;261789159;261789193;359773552;372911483&isWriteComm=1&autoplay=true&ua=http://tv.&api_key=&tlogoad=http://tv.sohu.com/upload/swf/empty.swf|http://tv.sohu.com/upload/swf/time.swf&topBarFull=1

            return UrlHelper.GetParameter(url, "nid");
        }
    }
}
